package cn.starlightsoftware.sherly.framework.tenant.config;

/**
 *
 * @author 谷子毅
 */public class TenantContextHolder  {

    /** 当前租户编号 */
    private static final ThreadLocal<String> TENANT_ID = new ThreadLocal<>();

    /** 是否忽略租户 */
    private static final ThreadLocal<Boolean> IGNORE = new ThreadLocal<>();


    public static String getTenantId() {
        return TENANT_ID.get();
    }

    public static String getRequiredTenantId() {
        String tenantId = getTenantId();
        if (tenantId == null) {
            throw new NullPointerException("TenantContextHolder 不存在租户编号！");
        }
        return tenantId;
    }

    public static void setTenantId(String tenantId) {
        TENANT_ID.set(tenantId);
    }

    public static void setIgnore(Boolean ignore) {
        IGNORE.set(ignore);
    }

    public static boolean isIgnore() {
        return Boolean.TRUE.equals(IGNORE.get());
    }

    public static void clear() {
        TENANT_ID.remove();
        IGNORE.remove();
    }


}
